package za.ac.wits.elen7045.persistence.datasource;

import za.ac.wits.elen7045.persistence.Environment;
import za.ac.wits.elen7045.persistence.PersistentObject;
import za.ac.wits.elen7045.persistence.datasource.sqlite.AccountSQLiteDataSource;
import za.ac.wits.elen7045.persistence.datasource.sqlite.CredentialSQLiteDataSource;
import za.ac.wits.elen7045.persistence.datasource.sqlite.CreditCardProviderStatementSQLiteDataSource;
import za.ac.wits.elen7045.persistence.datasource.sqlite.MunicipalityStatementSQLiteDataSource;
import za.ac.wits.elen7045.persistence.datasource.sqlite.SQLiteDataSource;
import za.ac.wits.elen7045.persistence.datasource.sqlite.ServiceProviderSQLiteDataSource;
import za.ac.wits.elen7045.persistence.datasource.sqlite.StatementSQLiteDataSource;
import za.ac.wits.elen7045.persistence.datasource.sqlite.TelcoServiceProviderStatementSQLiteDataSource;
import za.ac.wits.elen7045.persistence.datasource.sqlite.UserSQLiteDataSource;
import za.ac.wits.elen7045.persistence.model.Account;
import za.ac.wits.elen7045.persistence.model.Credential;
import za.ac.wits.elen7045.persistence.model.CreditCardProviderStatement;
import za.ac.wits.elen7045.persistence.model.MunicipalityStatement;
import za.ac.wits.elen7045.persistence.model.ServiceProvider;
import za.ac.wits.elen7045.persistence.model.Statement;
import za.ac.wits.elen7045.persistence.model.TelcoServiceProviderStatement;
import za.ac.wits.elen7045.persistence.model.User;

public abstract class DataSourceFactory {
	
	public static <T extends PersistentObject> DataSource<T> getDataSource(Class<T> clazz, Environment env) {
		// TODO: determine the type of dataSource that is used...
		DataSourceType type = DataSourceType.SQLite;
		/* retrieve the appropriate dataSource based on its type */
		DataSource<T> dataSource = null;
		if (type == DataSourceType.SQLite) {
			dataSource = getSQLiteDataSource(clazz);
			dataSource.setEnvironment(env);
		} else if (type == DataSourceType.MySQL) {
			// TODO: create implementation for MySQL
		}
		return dataSource;
	}
	
	@SuppressWarnings("unchecked")
	private static <T extends PersistentObject> SQLiteDataSource<T> getSQLiteDataSource(Class<T> clazz) {
		if (clazz == User.class) {
			return (SQLiteDataSource<T>) new UserSQLiteDataSource();
		} 
		else if (clazz == Account.class) {
			return (SQLiteDataSource<T>) new AccountSQLiteDataSource();
		} 
		else if (clazz == ServiceProvider.class) {
			return (SQLiteDataSource<T>) new ServiceProviderSQLiteDataSource();
		} 
		else if (clazz == Credential.class) {
			return (SQLiteDataSource<T>) new CredentialSQLiteDataSource();
		} 
		else if (clazz == Statement.class) {
			return (SQLiteDataSource<T>) new StatementSQLiteDataSource();
		} 
		else if (clazz == MunicipalityStatement.class) {
			return (SQLiteDataSource<T>) new MunicipalityStatementSQLiteDataSource();
		} 
		else if (clazz == CreditCardProviderStatement.class) {
			return (SQLiteDataSource<T>) new CreditCardProviderStatementSQLiteDataSource();
		} 
		else if (clazz == TelcoServiceProviderStatement.class) {
			return (SQLiteDataSource<T>) new TelcoServiceProviderStatementSQLiteDataSource();
		}
		return null;
	}
	
}
